﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Text;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using KA41HLT;
namespace WPFExcelReport
{

    public partial class KA41HLTData
    {

        #region=============Cost===============
        //KAAUF,KAFOLGE,KAREZEPT,KASTBAT,KASTGA,KASTKE
        //KAAART,KAAVAR,KAAFARBE,KAABESCHI
        public DataSet LoadKAFOLGE(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * "
               + " FROM INTEX.KAFOLGE WHERE  KAFKANR = :KAFKANR AND KAFKAPOS=:KAFKAPOS"
              + " order by 1,2,3,4,5,6,11,12,13 desc";
                or1.Parameters.Add("KAFKANR", OracleType.NVarChar).Value = dwInput["KAAKANR"].ToString();
                or1.Parameters.Add("KAFKAPOS", OracleType.NVarChar).Value = dwInput["KAAKAPOS"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KAFOLGE";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadKAREZEPT(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                //KAAKANR ,KAAKAPOS
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * "
               + " FROM INTEX.KAREZEPT WHERE  KARKANR = :KARKANR AND KARKAPOS=:KARKAPOS"
               + " order by 1 desc";
                or1.Parameters.Add("KARKANR", OracleType.NVarChar).Value = dwInput["KAAKANR"].ToString();
                or1.Parameters.Add("KARKAPOS", OracleType.NVarChar).Value = dwInput["KAAKAPOS"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KAREZEPT";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadKASTBAT(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                //KAAKANR ,KAAKAPOS
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * "
               + " FROM INTEX.KASTBAT WHERE  KBAKANR = :KBAKANR AND KBAKAPOS=:KBAKAPOS"
             + " order by 1 desc";
                or1.Parameters.Add("KBAKANR", OracleType.NVarChar).Value = dwInput["KAAKANR"].ToString();
                or1.Parameters.Add("KBAKAPOS", OracleType.NVarChar).Value = dwInput["KAAKAPOS"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KASTBAT";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadKASTGA(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                //KAAKANR ,KAAKAPOS
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * "
               + " FROM INTEX.KASTGA WHERE  KGAKANR = :KGAKANR AND KGAKAPOS=:KGAKAPOS"
              + " order by 1,2,3,4,5,6,7 desc";
                or1.Parameters.Add("KGAKANR", OracleType.NVarChar).Value = dwInput["KAAKANR"].ToString();
                or1.Parameters.Add("KGAKAPOS", OracleType.NVarChar).Value = dwInput["KAAKAPOS"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KASTGA";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadKASTKE(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                //KAAKANR ,KAAKAPOS
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * "
               + " FROM INTEX.KASTKE WHERE  KKEKANR = :KKEKANR AND KKEKAPOS=:KKEKAPOS "
                + " order by 1 desc";
                or1.Parameters.Add("KKEKANR", OracleType.NVarChar).Value = dwInput["KAAKANR"].ToString();
                or1.Parameters.Add("KKEKAPOS", OracleType.NVarChar).Value = dwInput["KAAKAPOS"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KASTKE";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadKAVERF(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                //KAAKANR ,KAAKAPOS
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * "
               + " FROM INTEX.KAVERF WHERE  KAVKANR = :KAVKANR AND KAVKAPOS=:KAVKAPOS "
                + " order by 1 desc";
                or1.Parameters.Add("KAVKANR", OracleType.NVarChar).Value = dwInput["KAAKANR"].ToString();
                or1.Parameters.Add("KAVKAPOS", OracleType.NVarChar).Value = dwInput["KAAKAPOS"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KAVERF";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadKAAUF(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string KAAART = dwInput["KAAART"].ToString(); ;
                string KAAVAR = dwInput["KAAVAR"].ToString(); ;
                string KAAFARBE = dwInput["KAAFARBE"].ToString(); ;
                string KAABESCHI = dwInput["KAABESCHI"].ToString(); ;

                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * "
               + " FROM INTEX.KAAUF "
                + " WHERE  KAAART = '" + KAAART + "' AND KAAVAR='" + KAAVAR + "' AND KAAFARBE='" + KAAFARBE
                + "' AND KAABESCHI='" + KAABESCHI + "'"
               + " order by 1 desc";
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KAAUF";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet ListQuatation(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataRow dwART = null;
            try
            {
                setResult = LoadKAAUF(dwInput);
                setReturn.Merge(setResult);
                foreach (DataRow dwTemp in setReturn.Tables["KAAUF"].Rows)
                {

                    setResult = LoadKAFOLGE(dwTemp);
                    setReturn.Merge(setResult);
                    setResult = LoadKAREZEPT(dwTemp);
                    setReturn.Merge(setResult);
                    setResult = LoadKASTBAT(dwTemp);
                    setReturn.Merge(setResult);
                    setResult = LoadKASTGA(dwTemp);
                    setReturn.Merge(setResult);
                    setResult = LoadKASTKE(dwTemp);
                    setReturn.Merge(setResult);
                    setResult = LoadKAVERF(dwTemp);
                    setReturn.Merge(setResult);

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet GetYarnPrice(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tbLocal = new DataTable();
            try
            {
                string art = dwInput["QUARTICLE"].ToString();
                string var = dwInput["QUVAR"].ToString();
                string color = dwInput["QUCOLOR"].ToString();
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT ROWNUM AS YID,'0' AS YSUPID,'HLT' AS YSUPNAME,GWBART AS YYARN,GWBVAR AS YVAR,GWBFARBE AS YCOLOR"
                    + ", GWPRVH*0.01 AS YPPRICE,GWPRAK*0.01 AS YCPRICE,GWPRKU*0.01 AS YFPRICE,'KG' AS YUOM, GWAENDDAT AS YEFFECTDATE,"
                    + "GWNEUBEN AS YUSER, GWNEUDAT AS YDATE,"
                    + "'' AS YREMARK1,'' AS YREMARK2,'' AS  YREMARK3"
                    + " FROM GWERT WHERE   GWPART='EP   ' AND GWBART=:GWBART";
                or1.Parameters.Add("GWBART", OracleType.NVarChar).Value = art;
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KAAUF";
                    tbLocal = setResult.Tables[0];
                }
                if (tbLocal.Rows.Count == 0)
                    return setReturn;
                string cond = "YYARN='" + art + "' and YVAR='" + var + "' and YCOLOR='" + color + "'";
                DataRow[] rows = tbLocal.Select(cond);
                DataTable yarnp = new DataTable();
                if (rows.Length > 0)
                {
                    yarnp = rows.CopyToDataTable();
                    yarnp.TableName = "KAAUF";
                }
                setReturn.Merge(yarnp);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        #endregion
        #region========COMPUTE===============
        int maxid = 1;
        public Int64 GetQUID()
        {
            Int64 result = maxid++;
            try
            {
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        public DataSet BuidProcessCost(SETKA41HLT source, DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            setReturn.Merge(setData.QUPROCESS);
            DataTable tbMaster = source.Tables["QUTMASTER"];
            DataTable tbProd = setReturn.Tables["QUPROCESS"];
            DataTable tbProcess = source.Tables["PROCESSING"];
            DataRow[] rows = null;
            string cond = "PSTAGE='";
            try
            {
                int stage = 0;
                DataRow dwProd = null;
                int id = 1;
                ArrayList Listprocess = new ArrayList();
                foreach (DataRow dwMaster in tbMaster.Rows)
                {
                    stage = System.Convert.ToInt16(dwMaster["QUSTAGE"].ToString());
                    cond = "PSTAGE='" + stage + "'";
                    if (stage == 21 || stage == 22)
                    {
                        stage = 2;
                        cond = "PSTAGE='" + stage + "'";
                    }
                    rows = tbProcess.Select(cond);
                    foreach (DataRow dwProcess in rows)
                    {
                        dwProd = tbProd.NewRow();
                        NewProdRow(ref dwProd, dwMaster, dwProcess["PROCESS"].ToString(), id, source);
                        id++;
                        tbProd.Rows.Add(dwProd);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        private void NewProdRow(ref DataRow dwInput, DataRow dwMaster, string proc, int id, SETKA41HLT source)
        {
            try
            {
                /*
                染纱 YD
                白浆 BJ
                浆染 JR
                网络 WD
                织造 WV
                防缩 YS
                丝光 SG
                抓剪 ZM
                磨毛 MM
                涂层 TC
                印花 YH
                整理 FN
                其他 OC
                分摊 FT
                 成品合格率 QC*/
                string art = dwMaster["QUARTICLE"].ToString();
                string var = dwMaster["QUVAR"].ToString();
                string color = dwMaster["QUCOLOR"].ToString();
                string finish = dwMaster["QUFINISH"].ToString();
                string stage = dwMaster["QUSTAGE"].ToString();
                string article = art.Trim() + "." + var.Trim() + "." + color.Trim() + "." + finish.Trim();
                string procdes = proc;
                var query = from dwproc in source.Tables["PROCESSING"].AsEnumerable()
                            where dwproc["PROCESS"].ToString().Trim() == proc.Trim()
                            select dwproc;
                foreach (DataRow dwproc in query)
                {
                    procdes = dwproc["PROCDES"].ToString().Trim();
                }
                dwInput["QPID"] = GetQUID();
                dwInput["QPORDER"] = dwMaster["QUORDER"];
                dwInput["QPLINE"] = dwMaster["QULINE"];
                dwInput["QPPOS"] = id;
                dwInput["QPSTAGE"] = stage;
                dwInput["QPPROCESS"] = proc;
                dwInput["QPPROCDES"] = procdes;
                dwInput["QPMASTERID"] = dwMaster["QUBOMID"].ToString();
                dwInput["QPQUANTITY"] = dwMaster["QUQUANTITY"];
                dwInput["QPQTY1"] = dwMaster["QUQTY1"];
                dwInput["QPQTY2"] = dwMaster["QUQTY2"];
                dwInput["QPQTY3"] = dwMaster["QUQTY3"];
                dwInput["QPUOM"] = dwMaster["QUUOM"];
                dwInput["QPCVALUE"] = 0;
                dwInput["QPCPRICE"] = 0;
                dwInput["QPRATIO"] = 1;
                dwInput["QPUSER"] = UserID;
                dwInput["QPDATE"] = iDate;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public DataSet BuidMatBOMCost(SETKA41HLT source, DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            string cond = "", vart = "", vvar = "", vcolor = "", vfinish = "", order = "", line = "";
            double qty = 0.0;
            double bomratio = 0.0;
            DataRow[] rows = null;
            setReturn.Merge(setData.QUTMASTER);
            DataTable tbMaster = setReturn.Tables["QUTMASTER"];
            try
            {
                //读取BOM，产生报价结构
                /*
                <TextBlock>阶段为物料本身的状态，销售物料为9，成品为6，坯布为5，织轴为4，色纱为2，原纱为1</TextBlock>
                <TextBlock>BOMID为本物料作为上层需求时的ID，例如成品需求坯布BOMID为601，则坯布物料具有601的ID。</TextBlock>
                <TextBlock>依次为销售需求901，成品需求601，坯布需求501，织轴需求401，色纱需求301,201,101,原纱需求000</TextBlock>
                dwTemp["QUARTICLE"] = dwUI["KAAART"];
                dwTemp["QUVAR"] = dwUI["KAAVAR"];
                dwTemp["QUCOLOR"] = dwUI["KAAFARBE"];
                dwTemp["QUFINISH"] = dwUI["KAABESCHI"];
                 */
                order = dwInput["QUORDER"].ToString();
                line = dwInput["QULINE"].ToString();
                vart = dwInput["QUARTICLE"].ToString();
                vvar = dwInput["QUVAR"].ToString();
                vcolor = dwInput["QUCOLOR"].ToString();
                vfinish = dwInput["QUFINISH"].ToString();
                double fnratio = System.Convert.ToDouble(dwInput["QURATIO"].ToString());
                double ratio = 1;
                string varticle = vart.Trim() + "." + vvar.Trim() + "." + vcolor.Trim() + "." + vfinish.Trim();
                qty = System.Convert.ToDouble(dwInput["QUQUANTITY"].ToString());
                #region=======DataSource==================
                DataRow dwWASTAMM = null;
                DataRow dwWASTKE = null;
                DataTable tbWASTGA = source.WASTGA;
                DataTable tbBASTGA = source.BASTGA;
                cond = "WAART='" + vart + "' and WAVAR='" + vvar + "'";
                rows = source.WASTAMM.Select(cond);

                foreach (DataRow dwTemp in rows)
                {
                    dwWASTAMM = dwTemp;
                }
                #endregion

                #region======Sales=========
                DataRow dwVA = tbMaster.NewRow();

                dwVA["QUUSER"] = UserID;
                dwVA["QUDATE"] = iDate;
                dwVA["QUID"] = GetQUID();
                dwVA["QUORDER"] = order;
                dwVA["QULINE"] = line;
                dwVA["QUSTAGE"] = 9;
                dwVA["QUSTAGEDES"] = "分摊";
                dwVA["QUCVALUE"] = 0;
                dwVA["QUSOURCETYPE"] = "I";
                //自己的ID（BOMID），上一级的ID（SBOMID）
                dwVA["QUBOMID"] = "9." + varticle;
                dwVA["QUSBOMID"] = varticle;
                dwVA["QUARTICLE"] = vart;
                dwVA["QUVAR"] = vvar;
                dwVA["QUCOLOR"] = vcolor;
                dwVA["QUFINISH"] = vfinish;
                dwVA["QUQUANTITY"] = qty;
                dwVA["QURATIO"] = fnratio;
                tbMaster.Rows.Add(dwVA);
                #endregion

                #region======Finish=========
                DataRow dwWS05 = tbMaster.NewRow();

                dwWS05["QUUSER"] = UserID;
                dwWS05["QUDATE"] = iDate;
                dwWS05["QUID"] = GetQUID();
                dwWS05["QUORDER"] = order;
                dwWS05["QUCVALUE"] = 0;
                dwWS05["QULINE"] = line;
                dwWS05["QUSTAGE"] = 6;
                dwWS05["QUSTAGEDES"] = "整理";
                dwWS05["QUSOURCETYPE"] = "I";
                //自己的ID（BOMID），上一级的ID（SBOMID）
                dwWS05["QUBOMID"] = "6." + varticle;
                dwWS05["QUSBOMID"] = "9." + varticle;
                dwWS05["QUARTICLE"] = vart;
                dwWS05["QUVAR"] = vvar;
                dwWS05["QUCOLOR"] = vcolor;
                dwWS05["QUFINISH"] = vfinish;
                qty = System.Convert.ToDouble(dwVA["QUQUANTITY"].ToString());
                ratio = System.Convert.ToDouble(dwVA["QURATIO"].ToString());
                dwWS05["QUQUANTITY"] = System.Math.Round(qty / ratio, 2);
                dwWS05["QURATIO"] = 1;
                tbMaster.Rows.Add(dwWS05);
                #endregion

                #region======Greige=========
                DataRow dwWS03 = tbMaster.NewRow();
                string gart, gvar, gcolor;
                gart = dwWASTAMM["WAVWART"].ToString();
                gvar = dwWASTAMM["WAVWVAR"].ToString();
                gcolor = vcolor;
                string garticle = gart.Trim() + "." + gvar.Trim() + "." + gcolor.Trim();
                dwWS03["QUUSER"] = UserID;
                dwWS03["QUDATE"] = iDate;
                dwWS03["QUID"] = GetQUID();
                dwWS03["QUORDER"] = order;
                dwWS03["QULINE"] = line;
                dwWS03["QUCVALUE"] = 0;
                dwWS03["QUSTAGE"] = 5;
                dwWS03["QUSTAGEDES"] = "织造";
                dwWS03["QUSOURCETYPE"] = "I";
                //自己的ID（BOMID），上一级的ID（SBOMID）
                dwWS03["QUSBOMID"] = "6." + varticle;
                dwWS03["QUBOMID"] = "5." + garticle;
                dwWS03["QUARTICLE"] = gart;
                dwWS03["QUVAR"] = gvar;
                dwWS03["QUCOLOR"] = gcolor;
                bomratio = 0.01 * (100 + 0.01 * System.Convert.ToDouble(dwWASTAMM["WALAEAEND"].ToString()));
                qty = System.Convert.ToDouble(dwWS05["QUQUANTITY"].ToString());
                dwWS03["QUQUANTITY"] = System.Math.Round(qty / bomratio, 2);
                double weftdensity = 0.01 * System.Convert.ToDouble(dwWASTAMM["WARFDDIS"].ToString());
                string WAFDDIM = dwWASTAMM["WAFDDIM"].ToString().Trim();
                if (WAFDDIM == "INC")
                    weftdensity = weftdensity / 2.54;
                dwWS03["QUQTY1"] = System.Math.Round(qty * weftdensity, 1);
                dwWS03["QURATIO"] = 1;
                tbMaster.Rows.Add(dwWS03);
                #endregion

                #region======SizingBeam=========
                //WAKART WAKVAR WAKWCOL

                cond = "WAKART='" + vart + "' and WAKVAR='" + vvar + "' and WAKWCOL='" + vcolor + "'";
                rows = source.WASTKE.Select(cond);

                foreach (DataRow dwTemp in rows)
                {
                    dwWASTKE = dwTemp;
                }
                DataRow dwBS03 = tbMaster.NewRow();

                dwBS03["QUUSER"] = UserID;
                dwBS03["QUDATE"] = iDate;
                dwBS03["QUID"] = GetQUID();
                dwBS03["QUORDER"] = order;
                dwBS03["QULINE"] = line;
                dwBS03["QUSTAGE"] = 4;
                dwBS03["QUSTAGEDES"] = "浆纱";
                dwBS03["QUSOURCETYPE"] = "I";
                dwBS03["QUSBOMID"] = "5." + garticle;
                //自己的ID（BOMID），上一级的ID（SBOMID）
                string bart = dwWASTKE["WAKKART"].ToString().Trim();
                string bvar = dwWASTKE["WAKKVAR"].ToString().Trim();
                string bcolor = dwWASTKE["WAKKFARBE"].ToString().Trim();
                string barticle = bart + "." + bvar + "." + bcolor;
                dwBS03["QUBOMID"] = "4." + barticle;
                dwBS03["QUARTICLE"] = bart;
                dwBS03["QUCVALUE"] = 0;
                dwBS03["QUVAR"] = bvar;
                dwBS03["QUCOLOR"] = bcolor;
                bomratio = 0.001 * System.Convert.ToDouble(dwWASTKE["WAKEAFAKTR"].ToString());
                qty = System.Convert.ToDouble(dwWS03["QUQUANTITY"].ToString());
                dwBS03["QUQUANTITY"] = System.Math.Round(qty / bomratio, 2);
                dwBS03["QURATIO"] = 1;
                tbMaster.Rows.Add(dwBS03);
                #endregion

                #region======Weftyarn=========
                string stage = "EK";
                DataRow dwGS07 = null;
                cond = "WAGART='" + gart + "' and WAGVAR='" + gvar + "' and WAGWCOL='" + gcolor + "'";
                rows = tbWASTGA.Select(cond);
                string yart = "", yvar = "", ycolor = "", ysource = "", sbomid = "201";
                DataTable tbGSDAT = source.Tables["GSDAT"];
                string srctype = "I";
                string jw = "W";
                foreach (DataRow dwTemp in rows)
                {
                    srctype = "I";
                    sbomid = "301";
                    yart = dwTemp["WAGGART"].ToString();
                    yvar = dwTemp["WAGGVAR"].ToString();
                    ycolor = dwTemp["WAGGFARBE"].ToString();
                    var qyarn = from dwYarn in tbGSDAT.AsEnumerable()
                                where dwYarn["GSART"].ToString() == yart
                                & dwYarn["GSVAR"].ToString() == yvar
                                select dwYarn;
                    foreach (DataRow dwYarn in qyarn)
                    {
                        ysource = dwYarn["GSFERTKZ"].ToString();
                    }
                    if (ysource == "Z")
                        srctype = "P";
                    dwGS07 = tbMaster.NewRow();
                    dwGS07["QUUSER"] = UserID;
                    dwGS07["QUDATE"] = iDate;
                    dwGS07["QUID"] = GetQUID();
                    dwGS07["QUORDER"] = order;
                    dwGS07["QULINE"] = line;
                    dwGS07["QUJW"] = jw;
                    dwGS07["QUSOURCETYPE"] = srctype;
                    dwGS07["QUSTAGE"] = 21;
                    dwGS07["QUSTAGEDES"] = "纬纱";
                    dwGS07["QUBOMID"] = "21." + yart.Trim() + "." + yvar.Trim() + "." + ycolor.Trim();
                    //自己的ID（BOMID），上一级的ID（SBOMID）                    
                    dwGS07["QUSBOMID"] = "5." + garticle;
                    dwGS07["QUARTICLE"] = yart;
                    dwGS07["QUVAR"] = yvar;
                    dwGS07["QUCOLOR"] = ycolor;
                    dwGS07["QURATIO"] = 1;
                    dwGS07["QUCVALUE"] = 0;
                    dwGS07["QUQTY1"] = dwTemp["WAGFDZ"].ToString();

                    //WAGKGNET WAGKGBRUT WAGABF
                    bomratio = 0.01 * 0.001 * System.Convert.ToDouble(dwTemp["WAGKGBRUT"].ToString());
                    qty = System.Convert.ToDouble(dwWS03["QUQUANTITY"].ToString());
                    dwGS07["QUQUANTITY"] = System.Math.Round(qty * bomratio, 2);
                    tbMaster.Rows.Add(dwGS07);
                    if (srctype != "P")
                    {
                        setResult = GetRawYarn(source, dwGS07);
                        tbMaster.Merge(setResult.Tables["QUTMASTER"]);
                    }
                }
                #endregion

                #region======Warpyarn=========
                cond = "BAGART='" + bart + "' and BAGVAR='" + bvar + "' and BAGFARBE='" + bcolor + "'";
                rows = tbBASTGA.Select(cond);

                foreach (DataRow dwTemp in rows)
                {
                    sbomid = "301";
                    stage = "2";
                    srctype = "I";
                    yart = dwTemp["BAGGART"].ToString();
                    yvar = dwTemp["BAGGVAR"].ToString();
                    ycolor = dwTemp["BAGGFARBE"].ToString();
                    var qyarn = from dwYarn in tbGSDAT.AsEnumerable()
                                where dwYarn["GSART"].ToString() == yart
                                & dwYarn["GSVAR"].ToString() == yvar
                                select dwYarn;
                    foreach (DataRow dwYarn in qyarn)
                    {
                        ysource = dwYarn["GSFERTKZ"].ToString();
                    }
                    if (ysource == "Z")
                        srctype = "P";
                    dwGS07 = tbMaster.NewRow();
                    dwGS07["QUUSER"] = UserID;
                    dwGS07["QUDATE"] = iDate;
                    dwGS07["QUID"] = GetQUID();
                    dwGS07["QUORDER"] = order;
                    dwGS07["QULINE"] = line;
                    dwGS07["QUJW"] = "J";
                    dwGS07["QUSOURCETYPE"] = srctype;
                    dwGS07["QUSTAGE"] = "22";
                    dwGS07["QUSTAGEDES"] = "经纱";
                    dwGS07["QUBOMID"] = "22." + yart.Trim() + "." + yvar.Trim() + "." + ycolor.Trim();
                    dwGS07["QUSBOMID"] = "4." + barticle;
                    dwGS07["QUARTICLE"] = yart;
                    dwGS07["QUVAR"] = yvar;
                    dwGS07["QUQTY1"] = dwTemp["BAGFDZ"].ToString();
                    dwGS07["QUCOLOR"] = ycolor;
                    dwGS07["QURATIO"] = 1;
                    dwGS07["QUCVALUE"] = 0;
                    //WAGKGNET WAGKGBRUT WAGABF
                    bomratio = 0.01 * 0.001 * System.Convert.ToDouble(dwTemp["BAGKGBRUT"].ToString());
                    qty = System.Convert.ToDouble(dwBS03["QUQUANTITY"].ToString());
                    dwGS07["QUQUANTITY"] = System.Math.Round(qty * bomratio, 2);
                    tbMaster.Rows.Add(dwGS07);
                    if (srctype != "P")
                    {
                        setResult = GetRawYarn(source, dwGS07);
                        tbMaster.Merge(setResult.Tables["QUTMASTER"]);
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        private DataSet GetRawYarn(DataSet source, DataRow dwInput)
        {
            string qyarn, qyvar, qycolor;
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataTable tbGSDAT = source.Tables["GSDAT"];
            DataTable tbGASTGA = source.Tables["GASTGA"];
            DataTable tbMaster = setData.QUTMASTER.Clone();
            DataTable tmpMaster = setData.QUTMASTER.Clone();
            try
            {
                bool flag = false;
                string stage = "XX";
                string ryarn, rvar, rcolor, qyarticle, rarticle, jw;
                DataRow[] rows = null;
                string order, line;
                string ysource = "";
                DataRow dwGS07 = null;
                order = dwInput["QUORDER"].ToString();
                line = dwInput["QULINE"].ToString();
                Queue NewYarn = new Queue();
                Queue yarnq = new Queue();
                yarnq.Enqueue(dwInput);
                string srctype = "I";
                string sbomid = dwInput["QUBOMID"].ToString();
                jw = "W";
                if (sbomid.StartsWith("22."))
                {
                    jw = "J";
                }
                while (!flag)
                {
                    string cond = "";
                    while (yarnq.Count > 0)
                    {
                        dwInput = (DataRow)yarnq.Dequeue();
                        qyarn = dwInput["QUARTICLE"].ToString();
                        qyvar = dwInput["QUVAR"].ToString();
                        qycolor = dwInput["QUCOLOR"].ToString();
                        qyarticle = qyarn.Trim() + "." + qyvar.Trim() + "." + qycolor.Trim();
                        string processed = "";
                        var qbom = from dwYarnBom in tbGASTGA.AsEnumerable()
                                   where dwYarnBom["GAGART"].ToString() == qyarn
                                   & dwYarnBom["GAGVAR"].ToString() == qyvar
                                   select dwYarnBom;
                        foreach (DataRow dwYarnBom in qbom)
                        {
                            //找到下一级档案
                            ryarn = dwYarnBom["GAGGART"].ToString();
                            rvar = dwYarnBom["GAGGVAR"].ToString();
                            rcolor = dwYarnBom["GAGGFARBE"].ToString();
                            rarticle = ryarn.Trim() + "." + rvar.Trim() + "." + rcolor.Trim();
                            if (processed.Contains(rarticle + ";"))
                                continue;
                            processed += rarticle + ";";
                            cond = "GSART='" + ryarn + "' and GSVAR='" + rvar + "'";
                            rows = tbGSDAT.Select(cond);
                            foreach (DataRow dwYarn in rows)
                            {
                                ysource = dwYarn["GSFERTKZ"].ToString();
                            }
                            #region=============rawyarn==============
                            srctype = "I";
                            if (ysource == "Z")
                            {
                                srctype = "P";
                            }
                            dwGS07 = tbMaster.NewRow();
                            dwGS07["QUUSER"] = UserID;
                            dwGS07["QUDATE"] = iDate;
                            dwGS07["QUID"] = GetQUID();
                            dwGS07["QUORDER"] = order;
                            dwGS07["QULINE"] = line;
                            dwGS07["QUSOURCETYPE"] = srctype;
                            dwGS07["QUSTAGE"] = "1";
                            dwGS07["QUJW"] = jw;
                            dwGS07["QUSBOMID"] = sbomid;
                            dwGS07["QUBOMID"] = rarticle;
                            dwGS07["QUARTICLE"] = ryarn;
                            dwGS07["QUVAR"] = rvar;
                            dwGS07["QUCOLOR"] = rcolor;
                            dwGS07["QURATIO"] = 1;
                            dwGS07["QUCVALUE"] = 0;
                            double bomratio = 0.0000001 * System.Convert.ToDouble(dwYarnBom["GAGKGBRUT"].ToString());
                            double qty = System.Convert.ToDouble(dwInput["QUQUANTITY"].ToString());
                            dwGS07["QUQUANTITY"] = System.Math.Round(qty * bomratio, 2);
                            #endregion
                            if (srctype == "I")
                            {
                                NewYarn.Enqueue(dwGS07);
                            }
                            else
                            {
                                tbMaster.Rows.Add(dwGS07);
                            }
                        }
                    }
                    if (NewYarn.Count > 0)
                    {
                        flag = false;
                        while (NewYarn.Count > 0)
                        {
                            dwInput = (DataRow)NewYarn.Dequeue();
                            yarnq.Enqueue(dwInput);
                        }
                    }
                    else
                    {
                        flag = true;
                    }
                }
                setReturn.Merge(tbMaster);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        #endregion
    }
}
